package com.iplant.basic.controller.cfg;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.iplant.base.po.Pagination;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.basic.controller.BasicController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.CFGService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.cfg.CFGCalendar;

@RestController
@RequestMapping("/api/Holiday")
public class HolidayController extends BasicController<CFGCalendar> {

    private static Logger logger = LoggerFactory.getLogger(HolidayController.class);

    public HolidayController() {
        super(CFGCalendar.class);
    }

    @Autowired
    CFGService mCFGService;

    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wWorkshopID = StringUtils.parseInt(request.getParameter("WorkshopID"), -1);
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"), -1);
            int wWorkAreaID = StringUtils.parseInt(request.getParameter("WorkAreaID"), -1);
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"), -1);
            int wYear = StringUtils.parseInt(request.getParameter("Year"), -1);
            int wMonth = StringUtils.parseInt(request.getParameter("Month"), -1);

            Calendar wStartDate = StringUtils.parseCalendar(request.getParameter("StartDate"));
            Calendar wEndDate = StringUtils.parseCalendar(request.getParameter("EndDate"));

            int wIsCreate = StringUtils.parseInt(request.getParameter("IsCreate"), -1);

            if (wYear < 2000 && wMonth <= 0 &&
                    wStartDate.compareTo(CalendarTool.getUsedDate()) <= 0 && wEndDate.compareTo(CalendarTool.getUsedDate()) <= 0) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<CFGCalendar>> wServiceResult = mCFGService.CFG_QueryHoliday(wBMSEmployee, wFactoryID,
                    wWorkshopID,wWorkAreaID,wStationID, wYear, wMonth, wStartDate, wEndDate, wIsCreate, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error("All", ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}